<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>vuex中使用modules用法 | 一枚前端小菜鸟的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="构建vuex创建整体目录结构  modules目录结构  modules子文件 12345678910111213141516171819202122232425262728293031&#x2F;&#x2F; .....src&#x2F;store&#x2F;modules&#x2F;addNum.jsconst state &#x3D; &amp;#123;    num: &amp;#x27;&amp;#x27;&amp;#125;;&#x2F;&#x2F; 执行state发生改变const muta">
<meta property="og:type" content="article">
<meta property="og:title" content="vuex中使用modules用法">
<meta property="og:url" content="https://gitee.com/weiyayun1995/weiyayun1995.git/2021/05/18/vuex%E4%B8%AD%E4%BD%BF%E7%94%A8modules%E7%94%A8%E6%B3%95/index.html">
<meta property="og:site_name" content="一枚前端小菜鸟的博客">
<meta property="og:description" content="构建vuex创建整体目录结构  modules目录结构  modules子文件 12345678910111213141516171819202122232425262728293031&#x2F;&#x2F; .....src&#x2F;store&#x2F;modules&#x2F;addNum.jsconst state &#x3D; &amp;#123;    num: &amp;#x27;&amp;#x27;&amp;#125;;&#x2F;&#x2F; 执行state发生改变const muta">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="c:/Users/Administrator/AppData/Roaming/Typora/typora-user-images/image-20210518170633127.png">
<meta property="og:image" content="c:/Users/Administrator/AppData/Roaming/Typora/typora-user-images/image-20210518171013783.png">
<meta property="article:published_time" content="2021-05-18T03:32:06.000Z">
<meta property="article:modified_time" content="2021-05-18T09:39:21.194Z">
<meta property="article:author" content="yywei">
<meta property="article:tag" content="菜鸟,前端,js,react,vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="c:/Users/Administrator/AppData/Roaming/Typora/typora-user-images/image-20210518170633127.png">
  
    <link rel="alternate" href="/atom.xml" title="一枚前端小菜鸟的博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">一枚前端小菜鸟的博客</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">yywei的遇坑记录</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS 订阅"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="搜索"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/weiyayun1995/weiyayun1995.git"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-vuex中使用modules用法" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/05/18/vuex%E4%B8%AD%E4%BD%BF%E7%94%A8modules%E7%94%A8%E6%B3%95/" class="article-date">
  <time class="dt-published" datetime="2021-05-18T03:32:06.000Z" itemprop="datePublished">2021-05-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      vuex中使用modules用法
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h4 id="构建vuex"><a href="#构建vuex" class="headerlink" title="构建vuex"></a>构建vuex</h4><p>创建整体目录结构</p>
<p><img src="C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210518170633127.png" alt="image-20210518170633127"></p>
<p>modules目录结构</p>
<p><img src="C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210518171013783.png" alt="image-20210518171013783"></p>
<p>modules子文件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// .....src/store/modules/addNum.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> state = &#123;</span><br><span class="line">    num: <span class="string">&#x27;&#x27;</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 执行state发生改变</span></span><br><span class="line"><span class="keyword">const</span> mutations = &#123;</span><br><span class="line">    ADDNUM: <span class="function">(<span class="params">state, num</span>) =&gt;</span> &#123;</span><br><span class="line">        state.num = num</span><br><span class="line">    &#125;,</span><br><span class="line">    DELNUM: <span class="function">(<span class="params">state, num</span>) =&gt;</span> &#123;</span><br><span class="line">        state.num = num - <span class="number">1</span> </span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 触发state改变的唯一途径</span></span><br><span class="line"><span class="keyword">const</span> actions = &#123;</span><br><span class="line">    <span class="function"><span class="title">addNum</span>(<span class="params">&#123;commit&#125;, num=<span class="number">0</span></span>)</span> &#123;</span><br><span class="line">        <span class="comment">// 通过commit触发mutations</span></span><br><span class="line">        commit(<span class="string">&#x27;ADDNUM&#x27;</span>, num)</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="function"><span class="title">delNum</span>(<span class="params">&#123;commit&#125;, num</span>)</span> &#123;</span><br><span class="line">        commit(<span class="string">&#x27;DELNUM&#x27;</span>, num)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// namespaced:true必填</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    namespaced: <span class="literal">true</span>,</span><br><span class="line">    state,</span><br><span class="line">    mutations,</span><br><span class="line">    actions</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>modules中index.js文件，用来汇总子文件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// .....src/store/modules/index.js</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 此index.js文件的主要作用是将store中需要传入的modules参数统一管理</span></span><br><span class="line"><span class="keyword">import</span> addNum <span class="keyword">from</span> <span class="string">&#x27;./addNum&#x27;</span></span><br><span class="line">.....</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    addNum</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>store中的getters.js文件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ..... src/store/getters</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// getters文件的主要作用，是将modules中各个文件中的state提取出来，再通过mapGetters引入到各个组件中，方便组件直接使用</span></span><br><span class="line"><span class="keyword">const</span> getters = &#123;</span><br><span class="line">    <span class="comment">// state指的是modules中传入的文件</span></span><br><span class="line">    num: <span class="function"><span class="params">state</span>=&gt;</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(state, <span class="string">&#x27;state&#x27;</span>) </span><br><span class="line">        <span class="keyword">return</span> state.addNum.num</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> getters</span><br></pre></td></tr></table></figure>

<p>store中的主文件index.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ..... src/store/index.js</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//创建store全局作用仓库</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span></span><br><span class="line"><span class="keyword">import</span> modules <span class="keyword">from</span> <span class="string">&#x27;./modules&#x27;</span></span><br><span class="line"><span class="keyword">import</span> getters <span class="keyword">from</span> <span class="string">&#x27;./getters&#x27;</span></span><br><span class="line"><span class="comment">// Vuex作为插件 挂载到Vue实例上</span></span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"><span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">    modules, <span class="comment">//es6的写法，modules:modules</span></span><br><span class="line">    getters</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> store</span><br></pre></td></tr></table></figure>

<p>将store注入main.js中</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&#x27;./store&#x27;</span></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  el: <span class="string">&#x27;#app&#x27;</span>,</span><br><span class="line">  store,</span><br><span class="line">  template: <span class="string">&#x27;&lt;App/&gt;&#x27;</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<h4 id="如何使用vuex"><a href="#如何使用vuex" class="headerlink" title="如何使用vuex"></a>如何使用vuex</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 其中第一个addNum表示的是在modules的index.js文件中export的对象，第二个addNum表示对象中的方法(对应action)，参数表示的是将要传入到action文件中的值</span></span><br><span class="line"><span class="built_in">this</span>.$store.dispatch(<span class="string">&#x27;addNum/addNum&#x27;</span>,参数)</span><br><span class="line"></span><br><span class="line"><span class="comment">//mapGetters的用法</span></span><br><span class="line"><span class="keyword">import</span> &#123;mapGetters&#125; <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// num表示在getters文件中所定义相关state中的值</span></span><br><span class="line"><span class="comment">// mapGetters中传入的是数组，数组中的值可以直接使用，无需关心其变化vuex中的值发生改变其会做出相应的改变</span></span><br><span class="line">computed:&#123;</span><br><span class="line">    ...mapGetters([<span class="string">&#x27;num&#x27;</span>]),</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 查看store中相关的数据</span></span><br><span class="line"><span class="built_in">this</span>.$store.state</span><br></pre></td></tr></table></figure>


      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/weiyayun1995/weiyayun1995.git/2021/05/18/vuex%E4%B8%AD%E4%BD%BF%E7%94%A8modules%E7%94%A8%E6%B3%95/" data-id="ckq7mlsve000edgt9g1j40z0b" data-title="vuex中使用modules用法" class="article-share-link">分享</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/05/21/number%E6%95%B0%E5%AD%97%E8%BE%93%E5%85%A5%E6%A1%86%E5%B0%81%E8%A3%85/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">前一篇</strong>
      <div class="article-nav-title">
        
          number数字输入框封装
        
      </div>
    </a>
  
  
    <a href="/2021/05/18/axios%E4%BA%8C%E6%AC%A1%E5%B0%81%E8%A3%85/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">后一篇</strong>
      <div class="article-nav-title">axios二次封装</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">六月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/05/">五月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">三月 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/06/22/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E8%BD%AC%E6%8D%A2/">js数据类型及转换</a>
          </li>
        
          <li>
            <a href="/2021/06/18/react-hook-%E9%A1%B9%E7%9B%AE%E4%B8%AD%E5%AE%9E%E8%B7%B5/">react-hook 项目中实践</a>
          </li>
        
          <li>
            <a href="/2021/06/18/%E6%95%B0%E5%AD%97%E7%AD%BE%E5%90%8D/">数字签名</a>
          </li>
        
          <li>
            <a href="/2021/06/18/sendbeacon/">sendbeacon</a>
          </li>
        
          <li>
            <a href="/2021/06/18/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%88%A4%E6%96%AD/">js数据类型的判断</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 yywei<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>